<?php

namespace App\Console;

use App\Post;
use App\Postmeta;
use App\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\SwooleCommand::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $schedule->call(function () {
            $PostMeta = Postmeta::where("post_id",1)->get();
            foreach ($PostMeta as $m){
                $meta["$m->key"] = $m->value;
                $meta["$m->key"."_id"] = $m->id;
            }
            $initData = $this->GetNew('http://www.1demo.com:3000/api/news');
            $count = $initData->data->total - $meta["demoTotal"];
            if($count>0){
                $num = ceil($count/12);
                for ($i=$num;$i>0;$i--){
                    if($i>1){
                        $data = $this->GetNew('http://www.1demo.com:3000/api/news?page='.$i);
                    }else{
                        $data = $initData;
                    }
                    $n = count($data->data->data);
                    $user = User::find(1);
                    for($k=$n-1;$k>=0;$k--){
                        if($data->data->data[$k]->id>$meta["demoId"]){
                            $Post = new Post();
                            $con = $this->GetNew('http://www.1demo.com:3000/api/news/'.$data->data->data[$k]->id);
                            if($con->data){
                                $Post->user_id = 1;
                                $Post->xcx = 1;
                                $Post->tag = 35;
                                $Post->author = $user->name;
                                $Post->title = "【龙腾碑林】".$con->data->newsTitle;
                                $Post->cover = $con->data->imgSrc;
                                $Post->content = $this->strip_html_tags(array("div","a"),$con->data->content->content);
                                $Post->created_at = $con->data->dataDate;
                                $Post->save();
                                $meta["demoId"] = $data->data->data[$k]->id;
                            }
                        }else{
                            continue;
                        }
                    }

                }
                $PostMeta = Postmeta::find($meta["demoId_id"]);
                $PostMeta->value = $meta["demoId"];
                $PostMeta->save();
                $PostMeta = Postmeta::find($meta["demoTotal_id"]);
                $PostMeta->value = $initData->data->total;
                $PostMeta->save();
            }
        })->hourly();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }

    private function GetNew($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $json = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($json);
        return $json;
    }
    private function strip_html_tags($tags,$str){
        $html=array();
        foreach ($tags as $tag) {
            $html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i";
        }
        $data=preg_replace($html, '', $str);
        return $data;
    }
}
